package com.rentcars.controller;

import com.github.pagehelper.Page;
import com.rentcars.entity.CarRepair1;
import com.rentcars.entity.Result;
import com.rentcars.service.Repair1Service;
import com.rentcars.utils.DateUtils;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 汽车维修
 * @author 浩
 */
@RestController
public class RepairController {
    @Autowired
    @Getter
    @Setter
    private Repair1Service repair1Service;

    @RequestMapping("/getPage1")
    public Map queryRepair(int pageNum, int pageSize, CarRepair1 carRepair1) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("carRepair", carRepair1);
        Page page = repair1Service.qureyUsersByPage(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequestMapping("/repAdd")
    public Result add(CarRepair1 carRepair1) {
        repair1Service.insert(carRepair1);
        return new Result();
    }
    @RequestMapping("/repEditPage")
    public ModelAndView edit(String recordid) throws Exception{
        CarRepair1 carRepair1 = repair1Service.selectByPrimaryKey(recordid);
        Map<String,Object> map =new HashMap<>();
        Date returntime= DateUtils.getDate(carRepair1.getReturntime());
        Date usercartime= DateUtils.getDate(carRepair1.getUsecartime());
        map.put("recorid",recordid);
        map.put("carid", carRepair1.getCarid());
        map.put("headman", carRepair1.getHeadman());
        map.put("headmobile", carRepair1.getHeadmobile());
        map.put("initmile", carRepair1.getInitmile());
        map.put("initoil", carRepair1.getInitoil());
        map.put("remark", carRepair1.getRemark());
        map.put("repairaddress", carRepair1.getRepairaddress());
        map.put("repaircontent", carRepair1.getRepaircontent());
        map.put("repairmoney", carRepair1.getRepairmoney());
        map.put("returnmile", carRepair1.getReturnmile());
        map.put("returnoil", carRepair1.getReturnoil());
        map.put("returntime",returntime);
        map.put("usercartime",usercartime);
        return new ModelAndView("repair-edit",map);

    }

    @RequestMapping("/repDel")
    public Result del(String recordid) {
        repair1Service.deleteByPrimaryKey(recordid);
        return new Result();
    }
    @RequestMapping("/rEdit")
    public Result myEdit(CarRepair1 carRepair1) {
        repair1Service.updateByPrimaryKey(carRepair1);
        return new Result();
    }
    //主页面
    @RequestMapping("/r2")
    public ModelAndView getAll1() {
        return new ModelAndView("repair-list");
    }

    @RequestMapping("/r1")
    public ModelAndView getAll() {
        return new ModelAndView("repair-add");
    }
}
